<html>
<head><meta charset="utf-8"><title>What level are alignment requirements expressed? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html">What level are alignment requirements expressed?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197600798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197600798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197600798">(May 14 2020 at 18:48)</a>:</h4>
<blockquote>
<p>For example, &amp;u32 has forbidden values for address 0 (the null pointer) and for every address that is not a multiple of 4 (because u32 must be aligned to a multiple of 4 bytes).</p>
</blockquote>
<ul>
<li><a href="https://github.com/rust-lang/rfcs/pull/2888#issuecomment-628789625">An RFC repo issue</a></li>
</ul>
<p>This statement surprised me. I didn't think that the <em>language</em> said that <code>u32</code> needed to be aligned to 4 bytes, but that was a requirement of the <em>platform</em> (e.g. x64 or arm). I thought that Rust just said "it must be aligned" without stating what the alignment requirements are.</p>



<a name="197601160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197601160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197601160">(May 14 2020 at 18:50)</a>:</h4>
<p>I think this wording is just sloppy because in practice most platforms set alignment of u32 to 4, even if some obscure ones may set it to something different. Certainly the alignment of u64 varies among common platforms.</p>



<a name="197641779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197641779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197641779">(May 15 2020 at 02:25)</a>:</h4>
<p>I know 65816 uses alignment of 2 for everything larger than 1 byte, except pointers which have alignment 4(so that single loads/stores can't cross bank bounderies which is never correct) If rust required 4 for i32, that would have linkage issues, and cause soundness problems in my support library, which does create references to some 2-byte (but not 4-byte) aligned i32s</p>



<a name="197644088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197644088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197644088">(May 15 2020 at 03:13)</a>:</h4>
<p>oof, I'll be more careful to not assume align4 on even primitives XD</p>



<a name="197785230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197785230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197785230">(May 16 2020 at 08:55)</a>:</h4>
<p>Crucially though, whatever the platform says the alignment is is required <em>even when just creating a reference</em> that doesn't get used. I've seen Rust code that used <code>cfg</code> arguing "well on x86 we can do unaligned stuff". Such code is UB.<br>
(You are probably all aware, just making sure this is clear to anyone reading this.^^)</p>



<a name="197791002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791002">(May 16 2020 at 11:22)</a>:</h4>
<p>Hmm, I think that has reduced my confidence in my understanding, <span class="user-mention" data-user-id="120791">@RalfJ</span>  ;-)</p>
<blockquote>
<p>whatever the platform says the alignment is is required even when just creating a reference that doesn't get used</p>
</blockquote>
<p>This makes sense to me.</p>
<blockquote>
<p>used <code>cfg</code> arguing "well on x86 we can do unaligned stuff".</p>
</blockquote>
<p>This seems like it <em>should</em> have been OK; assuming that the x86 platform doesn't require the pointer to be aligned. Your sentence seems to indicate that more than current platform matters.</p>



<a name="197791082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791082">(May 16 2020 at 11:23)</a>:</h4>
<p>I guess the question is, what does it mean for "the x86 platform/target to require the ptr to be aligned"</p>



<a name="197791100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791100">(May 16 2020 at 11:23)</a>:</h4>
<p>some people might say this is a property of the CPU and has to do with whether an unaligned load/store instruction is permitted</p>



<a name="197791138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791138">(May 16 2020 at 11:24)</a>:</h4>
<p>and some people might say it has to do with what <code>align_of::&lt;i32&gt;()</code> will return on x86</p>



<a name="197791147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791147">(May 16 2020 at 11:24)</a>:</h4>
<p>if people check <code>align_of::&lt;i32&gt;() == 1</code> and then do unaligned stuff with an <code>i32</code>, they are indeed fine. no need to make any platform assumptions.</p>



<a name="197791158"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791158" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791158">(May 16 2020 at 11:25)</a>:</h4>
<p>but that's not what I meant above, they were doing unaligned loads even though <code>align_of::&lt;i32&gt;() == 4</code>. they were thinking that "load/store instruction does not require alignment"  would matter. it does not. the only thing that matters is what <code>align_of</code> says.</p>



<a name="197791205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791205">(May 16 2020 at 11:26)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> does that help?</p>



<a name="197791328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791328">(May 16 2020 at 11:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197791082">said</a>:</p>
<blockquote>
<p>I guess the question is, what does it mean for "the x86 platform/target to require the ptr to be aligned"</p>
</blockquote>
<p>Yes, this is probably a key point. It sounds like you are saying that it's tied to what <code>align_of</code> returns. I guess that opens up my next question — how is the return value of <code>align_of</code> decided upon?</p>



<a name="197791337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791337">(May 16 2020 at 11:31)</a>:</h4>
<p>This is a non-academic question for me, I think, as I'm helping a little with the AVR port.</p>



<a name="197791340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791340">(May 16 2020 at 11:31)</a>:</h4>
<blockquote>
<p>how is the return value of align_of decided upon?</p>
</blockquote>
<p>no idea. I think this is part of that magic string that you can find in every target definition. LLVM  might also come into play.</p>



<a name="197791344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791344">(May 16 2020 at 11:31)</a>:</h4>
<p>it might be part of the stuff one writes down when defining a "system ABI"</p>



<a name="197791345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791345">(May 16 2020 at 11:31)</a>:</h4>
<p>together with calling conventions and the like</p>



<a name="197791417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791417">(May 16 2020 at 11:33)</a>:</h4>
<p>On most platforms, for most primitive types types, it is determined by the C ABI we're trying to match. We can't set a lower ABI alignment than C because then struct/union layouts will be different and C code will run into UB when you hand it pointers to Rust-allocated data, and we can't set it higher for the same reasons in the other direction.</p>



<a name="197791475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791475">(May 16 2020 at 11:35)</a>:</h4>
<p>one "lower" should be "higher" I think</p>



<a name="197791478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791478">(May 16 2020 at 11:35)</a>:</h4>
<p>This "only" applies to types which are (directly or indirectly) allowed in FFI, but that turns out to cover almost all primitives, and for aggregates there's not really a good alternative to to taking the maximum of the alignments of the fields as the alignment of the aggregate.</p>



<a name="197791480"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791480" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791480">(May 16 2020 at 11:35)</a>:</h4>
<p>Right, edited.</p>



<a name="197791610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791610">(May 16 2020 at 11:38)</a>:</h4>
<p>See, we typed that magic LLVM string for AVR at one point. In our case, we were still trying to match what GCC did. Now I wonder what it would look like for a total blank slate.</p>



<a name="197791917"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791917" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791917">(May 16 2020 at 11:46)</a>:</h4>
<p>I think on modern x86, UB for unaligned accesses probably does more harm than good, but there's still incentives to generally align your data when you can. So the "blank slate" state I'm curious about is if we just had "preferred alignment" that influenced data layout but didn't lead to language level UB.</p>



<a name="197791935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791935">(May 16 2020 at 11:47)</a>:</h4>
<p>(we could actually kind of do that in Rust: leave <code>align_of</code> as-is for compatibility but don't add <code>align n</code> to references and memory accesses, other than atomics)</p>



<a name="197791989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197791989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197791989">(May 16 2020 at 11:48)</a>:</h4>
<p>(more precisely: introduce a second notion of alignment that can be smaller than ABI alignment and couple all our alignment related UB to this number instead of <code>align_of</code>)</p>



<a name="197792005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792005">(May 16 2020 at 11:50)</a>:</h4>
<p>OTOH, other platforms still trap on unaligned accesses, so this could be a portability hazard</p>



<a name="197792209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792209">(May 16 2020 at 11:54)</a>:</h4>
<p>The way I see it, it's better if code is fine on x86 and doesn't compile on MIPS than if it's UB everywhere but appears to work on x86</p>



<a name="197792214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792214">(May 16 2020 at 11:54)</a>:</h4>
<p>Or even if it compiles but traps in MIPS</p>



<a name="197792278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792278">(May 16 2020 at 11:56)</a>:</h4>
<p>oh so your suggestion for removal of UB was for all platforms, not just x86?</p>



<a name="197792287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792287">(May 16 2020 at 11:57)</a>:</h4>
<p>wouldn't this effectively mean codegen'ing every load and store like <code>read/write_unaligned</code>?</p>



<a name="197792453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792453">(May 16 2020 at 12:02)</a>:</h4>
<p>It would mean that, and that's too expensive on some platforms, so I'm not proposing that. I just wonder if we can reduce the incidence of programmers causing UB by doing unaligned accesses the "natural" way that they "know" is fine (at machine code level) on x86</p>



<a name="197792487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792487">(May 16 2020 at 12:03)</a>:</h4>
<p>we could do the change you are proposing but just for x86.</p>



<a name="197792499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792499">(May 16 2020 at 12:03)</a>:</h4>
<p>would changing <code>align_of</code> be such a problem, as long as allocators still followed "preferred alignment"? I admit I never understood that "preferred" thing. Miri entirely ignores it.</p>



<a name="197792501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792501">(May 16 2020 at 12:04)</a>:</h4>
<p>(Well, I'm not <em>just</em> worried about accidential UB, I would also like to see if algorithms that rely on unaligned accesses get much mor ergonomic to write if you don't need to carefully handle raw pointers and std::ptr functions.)</p>



<a name="197792548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792548">(May 16 2020 at 12:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124289">Hanna Kruppe</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197792501">said</a>:</p>
<blockquote>
<p>(Well, I'm not <em>just</em> worried about accidential UB, I would also like to see if algorithms that rely on unaligned accesses get much mor ergonomic to write if you don't need to carefully handle raw pointers and std::ptr functions.)</p>
</blockquote>
<p>is that ergonomic win worth it if it's just for one platform though?</p>



<a name="197792558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792558">(May 16 2020 at 12:05)</a>:</h4>
<p>maybe this should be filed into the general category of "raw ptr ergonomics", which already has a bunch of other stuff in it</p>



<a name="197792559"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792559" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792559">(May 16 2020 at 12:05)</a>:</h4>
<p>Well, align_of is what all Rust code talking to allocator uses, i don't think the preferred align is even exposed outside of the compiler.</p>



<a name="197792560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792560">(May 16 2020 at 12:05)</a>:</h4>
<p>as in, dont change what references do, but make raw (in particular, unaligned) ptrs less annoying to use</p>



<a name="197792608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792608">(May 16 2020 at 12:06)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124289">Hanna Kruppe</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197792559">said</a>:</p>
<blockquote>
<p>Well, align_of is what all Rust code talking to allocator uses, i don't think the preferred align is even exposed outside of the compiler.</p>
</blockquote>
<p>only <a href="https://doc.rust-lang.org/nightly/std/intrinsics/fn.pref_align_of.html">unstably</a></p>



<a name="197792611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792611">(May 16 2020 at 12:06)</a>:</h4>
<p>not sure why that still exists then, TBH^^</p>



<a name="197792752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197792752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197792752">(May 16 2020 at 12:10)</a>:</h4>
<blockquote>
<p>is that ergonomic win worth it if it's just for one platform though?</p>
</blockquote>
<p>It's plausibly multiple platforms, but anyway, these kinds of code paths are generally platform-specific anyway because they're written that way for performance reasons and the performance profile of unaligned accesses varies wildly between platforms. If you have a code path using unaligned accesses, you probably also have a different code path not using them, or you don't support any platforms where this is an issue.</p>



<a name="197793749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197793749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197793749">(May 16 2020 at 12:35)</a>:</h4>
<p>Compilers can and do take advantage of alignment information, even on x86: <a href="https://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html">https://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html</a></p>



<a name="197793800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197793800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197793800">(May 16 2020 at 12:37)</a>:</h4>
<p>If anyone tries to claim "but x86 supports unaligned memory access", just show them this counter-example.</p>



<a name="197793849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197793849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197793849">(May 16 2020 at 12:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197793800">said</a>:</p>
<blockquote>
<p>If anyone tries to claim "but x86 supports unaligned memory access", just show them this counter-example.</p>
</blockquote>
<p>thanks, I have a bookmark folder for stuff like that :)</p>



<a name="197794026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197794026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197794026">(May 16 2020 at 12:43)</a>:</h4>
<p>oh wow I didnt know C also has UB in unaligned ptr casts.</p>



<a name="197794164"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197794164" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197794164">(May 16 2020 at 12:47)</a>:</h4>
<p>This is a fine example for demonstrating that this UB can lead to actual problems and not just "de jure" incorrectness, but I want to point out that it isn't a good argument for why unaligned accesess <em>ought</em> to be UB on x86. Just using <code>movdqu</code> instead of <code>movdqa</code> makes the problem go away, and has zero performance impact (when data is aligned, i.e., when the <code>*a</code> variant works at all) on any uarch anyone cares about.</p>
<p>There are also other examples of compiler optimizations that are enabled by the assumption of alignment (e.g. optimizing out alignment checks in generic routines after a caller, or being able to prove some accesses must be non-overlapping in some niche cases), but as far as I know  there are all very niche and very small wins. Not enough to justify UB or to justify jumping through hoops when you <em>want</em> unaligned accesses.</p>



<a name="197796834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197796834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197796834">(May 16 2020 at 13:57)</a>:</h4>
<p>For a bit of prior art, VC++ has an <code>__unaligned</code> attribute that can be slapped on pointers to let you trivially manipulate unaligned data safely.</p>



<a name="197828882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197828882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197828882">(May 17 2020 at 03:56)</a>:</h4>
<p>Having the alignment requirement is good on 65816, because if I don't know the access is aligned, I have to copy the data 8-bits at a time into a dedicated memory address. This has performance and size issues. This is because the 65816 wraps accesses at 64k (bank) boundaries, even though it can address 16M.</p>



<a name="197830363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197830363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197830363">(May 17 2020 at 04:47)</a>:</h4>
<p>Also don't forget that Windows has separate concepts for default alignment vs required alignment. A type's actual alignment is the greater of its default alignment and its required alignment. By default all types have a required alignment of 1, and the default alignment is the type's normal natural alignment. If you use <code>#pragma pack</code> you are lowering the <em>default</em> alignment of any fields in that struct. If you use <code>__declspec(align)</code> you are raising the <em>required</em> alignment of any fields in the struct. Notably this means if you have any <code>__declspec(align)</code> types inside a <code>#pragma pack</code> struct, the alignment of those specific fields will <em>not</em> be lowered below their required alignment. This is completely contrary to what gcc does.</p>



<a name="197838001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197838001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197838001">(May 17 2020 at 08:40)</a>:</h4>
<p><span class="user-mention" data-user-id="125267">@Peter Rabbit</span> that last point seems relevant when compiling/interoperating with MSVC, but for Rust itself, it's just not how <code>repr(packed)</code> behaves -- right? What we do with reference UB is a purely Rust-internal decision as far as I can see.</p>



<a name="197838060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/197838060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Peter Rabbit <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#197838060">(May 17 2020 at 08:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197838001">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="125267">Peter Rabbit</span> that last point seems relevant when compiling/interoperating with MSVC, but for Rust itself, it's just not how <code>repr(packed)</code> behaves -- right? What we do with reference UB is a purely Rust-internal decision as far as I can see.</p>
</blockquote>
<p>It's the reason Rust currently forbids <code>repr(align)</code> types inside <code>repr(packed)</code>, but yes Rust choosing to make insufficiently aligned references UB is purely Rust's decision to make. I'm perfectly fine with it being UB as long as we have the tools to write reasonably ergonomic code that works with packed unaligned data.</p>



<a name="199367672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/199367672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Frank McSherry <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#199367672">(Jun 01 2020 at 13:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="143274">Amanieu</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F/near/197793800">said</a>:</p>
<blockquote>
<p>If anyone tries to claim "but x86 supports unaligned memory access", just show them this counter-example.</p>
</blockquote>
<p>I didn't read that in the article, but I might have misunderstood. The article seemed to be about how a language spec allowed UB for unaligned accesses, even though the underlying platform permitted it, and how this was the source of bugs. (( sorry for the drive-by; this just happened to be the focus post when I logged in, and as someone who wants to do zero-copy work on data received over the network I have a horse in this race. ))</p>



<a name="200514552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/200514552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#200514552">(Jun 11 2020 at 07:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116609">@Frank McSherry</span> I think that's the point: unaligned accesses are UB, and "what the hardware does" (aka "but x86 supports unaligned accesses") does not make it any less buggy to perform unaligned accesses. This <em>will</em> go wrong even on x86 when accesses are not properly aligned.</p>



<a name="200514582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What%20level%20are%20alignment%20requirements%20expressed%3F/near/200514582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/What.20level.20are.20alignment.20requirements.20expressed.3F.html#200514582">(Jun 11 2020 at 07:56)</a>:</h4>
<p>Last year I found code in <code>rand</code> that did <code>cfg(target = x86)</code> to then proceed and perform unaligned accesses. So even in the Rust community, this is a point that needs extra awareness.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>